// import "package:intl/intl.dart";
import 'package:decimal/decimal.dart';

class StringUtils {

  static bool isDouble(str) {
    try {
      if (str.toString() == 'Infinity') return false;
      double.parse(str.toString());
      return true;
    } catch (e) {
      return false;
    }
  }

  static DoubletoString(double1, index) {
    return double1;
  }

  static String emptyToDef(str, def) {
    if (isEmpty(str))
      return def;
    return str;
  }

  ///判断字符是否为空
  static bool isEmpty(str) {
    if (str == null) return true;
    if (str.toString() == 'null') return true;
    return str
        .toString()
        .isEmpty;
  }

  ///判断字符是否不为空
  static bool isNotEmpty(str) {
    return !isEmpty(str);
  }

  ///未空显示默认value
  static String defaultValue(str,DefaultValue) {
    if(isEmpty(str))return DefaultValue;
    return str;
  }


  ///四舍五入 保留小数
  static String toFixed(str, [point = 2]) {
    if (str == null) return "";
    try {
      double d;
      if (str is double)
        d = str;
      else
        d = double.parse(str);
      return Decimal.parse(d.toStringAsFixed(point)).toString();
    } catch (e) {
      return str;
    }
  }


  ///判定是否为数字
  static bool isNumeric(String s) {
    if (s == null) {
      return false;
    }
    return double.tryParse(s) != null;
  }

  ///格式化 積分類型
  static String formatUnitKw(number) {
    double number1 = 0;
    if (number is num)
      number1 = number.toDouble();
    if (number is String)
      number1 = double.parse(number);
    if (number1 >= 1000000) {
      number1 = number1 / 1000000;
      String s = number1.toStringAsFixed(1).toString() + "M";
      return s;
    } else if (number1 >= 1000) {
      number1 = number1 / 1000;
      return number1.toStringAsFixed(1).toString() + "K";
    }
    return number.toString();
  }

  String phoneHiding(String phone) {
    if (phone != null && phone.length > 7) {
      phone = phone.substring(0, 3) + "****" + phone.substring(7);
    }else{
      return "********";
    }
    return phone;
  }
}